package com.java110.mall.goods.cmd.product;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.po.product.ProductPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;

@Java110Cmd(serviceCode = "product.updateProductState")
public class UpdateProductStateCmd extends Cmd {

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        Assert.hasKeyAndValue(reqJson, "productId", "未包含商品");
        Assert.hasKeyAndValue(reqJson, "state", "未包含状态");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        ProductPo productPo = BeanConvertUtil.covertBean(reqJson, ProductPo.class);
        int flag = productInnerServiceSMOImpl.updateProduct(productPo);
        if (flag < 1) {
            throw new IllegalArgumentException("参数错误");
        }

    }
}
